<?php
session_start();

if(isset($_SESSION['user']))
{
	header('Location: logged.php');
}
?>
<center>
<br/><br/><br/><br/>
 <form action="<?php $_SERVER["PHP_SELF"]/*or login.php */?>" method="post" >
 <table>
    <tr>
        <td colspan=3 align=center>
                <h2><p style="color:blue">LOGIN</p></h2>
        </td>
    </tr>
    <tr>
        <td>
            Username:
        </td>
        <td width=20>
        </td>
        <td>
            <input type="text" name="user">
        </td>
    </tr>
    <tr>
        <td align=top>
            Password:
        </td>
                <td width=20>
        </td>
        <td>
            <input type="password" name="pass">     
        </td>
    </tr>
    <tr>
        <td colspan=3 align=center>          
            <input type="submit" value="Login">
        </td>
    </tr>
    <tr>
        <td colspan=3 align=center>          
          <?php 
		  include("Libraries/BusinessLogic/users.php");
		  include("Libraries/BusinessLogic/statheres.php");
		  if(isset($_POST["user"]))
		  { 
		  	$username=$_POST["user"];
    		$password=$_POST["pass"]; 
		    $result=login_user($username,$password);
			switch ($result['status']) 
			{
			case statheres::OLA_OK:
				$_SESSION['user']=$username;
				header('Location: logged.php');
				break;
			case statheres::WRONG_PASS_OR_USER:
				echo "<script>alert('Wrong password or username')</script>";
				echo "<center><h4><font color='red'><noscript>Wrong password or username</noscript></p></h4></font>";
				
				break;
			case statheres::EMPTY_FIELD:
				echo "<script>alert('Fill all fields!')</script>";
				echo "<noscript><center><h4><font color='red'>Fill all fields!</p></h4></font></noscript>";
				break;
			case statheres::LARGE_FIELD:
				echo "<script>alert('Too long password or username')</script>";
				echo "<noscript><center><h4><font color='red'>Too long password or username</p></h4></font></noscript>";
				break;
			default:
				echo "<script>alert('Unexpected error')</script>";
				echo "<noscript><center><h4><font color='red'>Unexpected error</p></h4></font></noscript>";
			}
		  }
		  ?>
        </td>
    </tr>
    <tr>
        <td colspan=3 align=center>          
        
        </td>
    </tr>
</table>
</form>